package info.rodinia.tokenmaker;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Power models D&D 4E powers, both those used by players and monsters
 * 
 * @author Blakey, Summer 2010
 */
public class Power {

    // compendium info
    private int id = 0;
    private String html = null;

    // Actual D&D stats side of things
    private String name = ""; // e.g. "Melee Basic Attack"
    private int level = 0;
    private String powerUsage = ""; // e.g. "At-Will", "Encounter"
    private String display = ""; // e.g. "Basic Attack", "Monk Attack 1"
    private String keywords = ""; // e.g. "Full Discipline, Implement, Psionic"
    private String actionType = ""; // e.g. "Standard Action", "Move"
    private String attackType = ""; // e.g. "Melee touch", "Close burst 1"
    private String target = ""; // e.g. "Each enemy you can see in burst"
    private String attack = ""; // e.g. "Dexterity vs. Reflex"
    private String hit = ""; // e.g. "1d8 + Dexterity modifier damage"
    private String miss = ""; // e.g. "Half damage."
    private String powerType = ""; // e.g. "Attack"
    private String effect = ""; // e.g. "You stand up."
    private String trigger = ""; // e.g. "You hit with an attack during your turn"
    private String special = ""; // e.g. "You can use this power only once per round."
    private String flavor = ""; // e.g. flavour text for this power.
    private String requirement = "";
    private String infernalPact = ""; // e.g. "Special infernal pact benefits."
    
    private ArrayList<PowerLine> lines = new ArrayList<PowerLine>();
    private ArrayList<PowerWeapon> weapons = new ArrayList<PowerWeapon>();
    private String detail = "";
    private boolean inSpellbook = false;

    public Power(Power copy) {
	setId(copy.getId());
	this.html = null;
	this.name = copy.name;
	this.level = copy.level;
	this.powerUsage = copy.powerUsage;
	this.display= copy.display;
	this.keywords= copy.keywords;
	this.actionType= copy.actionType;
	this.attackType= copy.attackType;
	this.target= copy.target;
	this.attack= copy.attack;
	this.hit= copy.hit;
	this.miss= copy.miss;
	this.powerType= copy.powerType;
	this.effect= copy.effect;
	this.infernalPact= copy.infernalPact;
	this.trigger = copy.trigger;
	this.special= copy.special;
	this.flavor= copy.flavor;
	copyWeapons(copy);
	copyLines(copy);
	this.detail = copy.detail;
    }
    
    // do a deep copy of the weapons
    private void copyWeapons(Power copy) {
	weapons = new ArrayList<PowerWeapon>();
	for (PowerWeapon w : copy.getWeapons()) {
	    PowerWeapon newpw = new PowerWeapon(w);
	    newpw.setPower(this);
	    weapons.add(newpw);
	}
    }
    
    // do a deep copy of the lines
    private void copyLines(Power copy) {
	lines = new ArrayList<PowerLine>();
	for (PowerLine l : copy.getLines()) {
	    PowerLine newpl = new PowerLine(l);
	    newpl.setPower(this);
	    lines.add(newpl);
	}
    }
    
    public Power(String name) {
	this.name = name;
    }

    public Power(int id) {
	this.id = id;
    }

    public String getHTML() {
	// If we have flavor filled out then we have all we need to build our HTML power from that.

	if (!flavor.equals("") || !effect.equals("")) {
	    buildHTML();
	    return html;
	}
	    
	if (id == 0)
	    return "";
	if (html == null) {
	    PowerCompendiumEntry com = new PowerCompendiumEntry(this);
	    html = com.getHTML();
	    html = html.replace("<img src=\"http://www.wizards.com/dndinsider/compendium/images/bullet.gif\" alt=\"\"/>", "-");
	}
	return html;
    }

    public String toString() {
	String result = name + " (" + actionType + ", " + powerUsage + ")";
	// if (weapons.size() != 0) result += weapons.get(0);
	return result;
    }

    public boolean isInSpellbook() {
        return inSpellbook;
    }

    public void setInSpellbook(boolean inSpellbook) {
        this.inSpellbook = inSpellbook;
    }

    public int getId() {
	return id;
    }

    public void setId(int id) {
	this.id = id;
    }

    public String getTrigger() {
        return trigger;
    }

    public void setTrigger(String trigger) {
        this.trigger = trigger;
    }

    public void setUsage(String usage) {
	this.powerUsage = usage;
    }

    public void setActionType(String actionType) {
	this.actionType = actionType;
    }

    public void addWeapon(PowerWeapon weapon) {
	weapons.add(weapon);
    }

    public void setWeapons(ArrayList<PowerWeapon> weapons) {
	this.weapons = weapons;
    }

    public void addLine(PowerLine line) {
	lines.add(line);
    }

    public void setLines(ArrayList<PowerLine> lines) {
	this.lines = lines;
    }

    public String getName() {
	return name;
    }

    public void setName(String name) {
	this.name = name;
    }

    public int getLevel() {
	return level;
    }

    public void setLevel(int level) {
	this.level = level;
    }

    public String getRequirement() {
        return requirement;
    }

    public void setRequirement(String requirement) {
        this.requirement = requirement;
    }

    public String getUsage() {
	return powerUsage;
    }

    public String getActionType() {
	return actionType;
    }

    public ArrayList<PowerWeapon> getWeapons() {
	return weapons;
    }

    public ArrayList<PowerLine> getLines() {
	return lines;
    }


    public String getDetail() {
	return detail;
    }

    public void setDetail(String detail) {
	this.detail = detail;
    }

    public String getHtml() {
        return html;
    }

    public void setHtml(String html) {
        this.html = html;
    }

    public String getPowerUsage() {
        return powerUsage;
    }

    public void setPowerUsage(String powerUsage) {
        this.powerUsage = powerUsage;
    }

    public String getDisplay() {
        return display;
    }

    public void setDisplay(String display) {
        this.display = display;
    }

    public String getKeywords() {
        return keywords;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    public String getAttackType() {
        return attackType;
    }

    public void setAttackType(String attackType) {
        this.attackType = attackType;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String getAttack() {
        return attack;
    }

    public void setAttack(String attack) {
        this.attack = attack;
    }

    public String getHit() {
        return hit;
    }

    public void setHit(String hit) {
        this.hit = hit;
    }

    public String getMiss() {
        return miss;
    }

    public void setMiss(String miss) {
        this.miss = miss;
    }

    public String getPowerType() {
        return powerType;
    }

    public void setPowerType(String powerType) {
        this.powerType = powerType;
    }

    public String getEffect() {
        return effect;
    }

    public void setEffect(String effect) {
        this.effect = effect;
    }

    public String getInfernalPact() {
        return infernalPact;
    }

    public void setInfernalPact(String infernalPact) {
        this.infernalPact = infernalPact;
    }

    public String getSpecial() {
        return special;
    }

    public void setSpecial(String special) {
        this.special = special;
    }

    public String getFlavor() {
        return flavor;
    }

    public void setFlavor(String flavor) {
        this.flavor = flavor;
    }
    
    // buildHTML should only be called when we have flavor set up already
    // so return if that is not true
    private void buildHTML() {
	if (flavor.equals("") && effect.equals(""))  return;
	
	html="<div id=\"detail\">\n";

	// Name and level of power
	if (powerUsage.equals("At-Will")) {
	    	html +="<h1 style=\"font:aerial;font-size:1.09em;font-weight: bold;line-height:2;padding-left:15px;margin:0;color:white;background: #619869;\"\n";
		html +="class=\"atwillpower\"><span style=\"font:aerial;display: block;margin-top: 0;text-align: right;position:relative;top:-60px;;\"\n";
	} else if (powerUsage.startsWith("Encounter")) {
	    	html +="<h1 style=\"font:aerial;font-size:1.09em;font-weight: bold;line-height:2;padding-left:15px;margin:0;color:white;background: #961334;\"\n";
		html +="class=\"encounterpower\"><span style=\"font:aerial;display: block;margin-top: 0;text-align: right;position:relative;top:-60px;;\"\n";
	} else if (powerUsage.equals("Daily")) {
	    	html +="<h1 style=\"font:aerial;font-size:1.09em;font-weight: bold;line-height:2;padding-left:15px;margin:0;color:white;background: #4d4d4f;\"\n";
		html +="class=\"dailypower\"><span style=\"font:aerial;display: block;margin-top: 0;text-align: right;position:relative;top:-60px;;\"\n";
	}
	html +="class=\"level\">"+display+"</span><br/>"+name+"</h1>\n\n";

	// Flavour Text
	html +="<p style=\"font:aerial;display: block;padding: 2px 15px;margin: 0;background: #d6d6c2;\"";
	html +="class=\"flavor\"><i>"+flavor+"</i></p>";
	
	// Base stats
	html +="<p style=\"font:aerial;padding: 0px 0px 0px 15px;margin: 0;background: #FFFFFF;\"";
	html +="class=\"powerstat\"><b>"+powerUsage+"</b>";
	if (!keywords.equals("")) html += "&nbsp;&nbsp; -&nbsp;&nbsp;&nbsp;&nbsp; <b>" + keywords + "</b>";
	html += "<br/>";
	
	html +="<b>"+actionType+"</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	if (!attackType.equals("")) html += attackType + "<br />";


	// Write out the power in compendium format: -
	for (PowerLine line : getLines()) {
	    if (line.getName().contains("Miss")) {
		html +="<p style=\"font:aerial;display: block;padding: 2px 15px;margin: 0;background: #FFFFFF;\"";
	    }
	    else {
		html +="<p style=\"font:aerial;display: block;padding: 2px 15px;margin: 0;background: #d6d6c2;\"";
	    }
	    html +="class=\"flavor\">";
	    html += "<b>"+ line.getName().replace(" ", "&nbsp;&nbsp;") +"</b>:&nbsp;" + line.getContent().replace("\n","<br/>\n") + "<br />";
	}
	html +="</p>";
	html +="</div>";
    }
}
